<template>
  <el-dialog
    v-el-drag-dialog
    title="退单确认"
    :visible.sync="visible"
    width="600px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :model="temp" label-position="right" label-width="80px" style="margin: 0 50px;">
      <el-form-item label="客户名称" class="block">
        <el-input v-model="temp.csmName" disabled />
      </el-form-item>
      <el-form-item label="客户地址" class="block">
        <el-input v-model="temp.csmAddress" disabled />
      </el-form-item>
      <el-form-item class="block" label="退单理由" prop="refundReason">
        <el-input v-model="temp.refundReason" type="textarea" :rows="3" maxlength="250" show-word-limit />
      </el-form-item>
      <el-form-item v-if="isLinePay" label-width="0">
        <el-checkbox v-model="checked">操作提示：此订单已线上支付,将同时产生退款申请单</el-checkbox>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryRefundOrderBack, queryRefundOrderDetailData } from '@/api'

  export default {
    name: 'back-order',
    data() {
      return {
        visible: false,
        isLinePay: false,
        orderCode: '',
        checked: true,
        temp: {
          refundReason: ''
        },
        detail: {}
      }
    },
    methods: {
      open(temp) {
        this.getOrderDetail(temp.orderCode)
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(this.temp, temp, {
              refundReason: temp.remark
            })
          }
        })
      },
      async getOrderDetail(orderCode) {
        const res = await queryRefundOrderDetailData({ orderCode })
        this.detail = JSON.parse(res.data.orderBillInfo)
        this.isLinePay = JSON.parse(res.data.isLinePay)
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          let payStatus = 0
          if (this.checked) {
            payStatus = this.detail.payStatus
          }
          const params = {
            orderCode: this.temp.orderCode,
            payStatus: payStatus,
            refundReason: this.temp.refundReason
          }
          this.submit(params)
        })
      },
      async submit(params) {
        await queryRefundOrderBack(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '操作成功!',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.temp = {
          refundReason: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  & /deep/ .el-form-item {
    width: 48%;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
</style>
